<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <div id="app"></div>
  <script src="../react.js"></script>
  <script src="../react-dom.js"></script>
  <script>
    // 生成一个组件
    var Component = React.createClass({
      // 设置属性类型
      propTypes: {
        text: React.PropTypes.string.isRequired
      },
      // 设置默认数据
      getDefaultProps: function () {
        return {
          text: 'default'
        }
      },
      // 添加监听
      getInitialState: function () {
        return {
          text: this.props.text
        }
      },
      // 事件监听
      _textChange: function (ev) {
        this.setState({
          text: ev.target.value
        })
      },
      _btnClick: function (ev) {
        console.log(this.state.text)
      },
      render: function () {
        return React.DOM.div(null, React.DOM.input({
            value: this.state.text,
            onChange: this._textChange
          }),
          React.DOM.div({
            style: {
              color: 'red'
            }
          }, this.state.text.length),
          React.DOM.button({
            value: '点击',
            style: {
              width: '120px',
              height: '44px',
              fontSize: '16px'
            },
            onClick: this._btnClick
          }, '点击'))
      }
    });
    ReactDOM.render(
      React.createElement(Component, {
        text: 'MyInput'
      }),
      document.getElementById('app')
    )
  </script>

</body>

</html>